package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.DictDataVo;
import com.data.core.dao.DictDataMapper;
import com.data.core.service.DictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.data.core.entry.DictData;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
* @version: V1.0
* @author: szw
* @className: DictDataServiceImpl
* @packageName: DictDataServiceImpl
* @description: 字典接口实现
* @data: 2021-1-19 
**/  
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Resource
    private DictDataMapper dictDataMapper;

    /**
     * 更新、保存方法实现
     * @param dictDataVo
     * @param flag  更新or保存
     * @return
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-更新、保存方法实现",operationType=BaseEnum.UPDATE_OR_SAVE)
    public int saveOrUpdateDictData(DictDataVo dictDataVo, Boolean flag) {
        if (Objects.isNull(dictDataVo)){
            throw new BaseException(BaseEnum.DICT_DATA_IS_NULL);
        }
        try{
            DictData dictData = new DictData();
            BeanUtils.copyProperties(dictDataVo,dictData);
            this.saveOrUpdateDictData(dictData,flag);
        }catch (Exception e){
            throw new BaseException(flag==true?BaseEnum.SAVE_DICT_DATA_IS_ERROR:BaseEnum.UPDATE_DICT_DATA_IS_ERROR);
        }
        return 0;
    }

    /**
     * 查询字典
     * @param id
     * @return
     */
    @Override
    @ApiLogDetail(detail = "核心服务-查询字典",operationType=BaseEnum.SELECT)
    public DictData getByDictDataId(Long id) {
        if (null==id){throw new BaseException(BaseEnum.DICT_DATA_ID_IS_NULL);}
        try{
          return dictDataMapper.selectById(id);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_DICT_DATA_IS_ERROR);
        }
    }

    /**
     * 删除字典方法
     * @param id
     * @return
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-删除字典方法",operationType=BaseEnum.DELETE)
    public int delete(Long id) {
        if (null==id){throw new BaseException(BaseEnum.DICT_DATA_ID_IS_NULL);}
        try{
            return dictDataMapper.deleteById(id);
        }catch (Exception e){
            throw new BaseException(BaseEnum.DELETE_DICT_DATA_IS_ERROR);
        }
    }

    /**
     * 字典分页列表
     * @param page
     * @param dictDataVo
     * @return
     */
    @Override
    @ApiLogDetail(detail = "核心服务-字典分页列表",operationType=BaseEnum.SELECT)
    public IPage<DictData> dictDataPage(Page<DictData> page, DictDataVo dictDataVo) {
        if (Objects.isNull(dictDataVo)){
            throw new BaseException(BaseEnum.DICT_DATA_IS_NULL);
        }
        try {
            QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("enable_flag",dictDataVo.getEnableFlag());
            return  dictDataMapper.selectPage(page,queryWrapper);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_DICT_DATA_IS_ERROR);
        }
    }

    /**
     * 根据状态查询字典列表信息
     * @param dictDataVo
     * @return
     */
    @Override
    @ApiLogDetail(detail = "核心服务-根据状态查询字典列表信息",operationType=BaseEnum.SELECT)
    public List<DictData> getDictDataList(DictDataVo dictDataVo) {
        if (Objects.isNull(dictDataVo)){ throw new BaseException(BaseEnum.DICT_DATA_IS_NULL);}
        try {
            QueryWrapper<DictData> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("enable_flag",dictDataVo.getEnableFlag());
            return dictDataMapper.selectList(queryWrapper);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_DICT_DATA_IS_ERROR);
        }
    }

    /**
     *  flag= true 保存
     *  flag= false 更新
     * @param dictData
     * @param flag
     * @return
     */
    private int saveOrUpdateDictData(DictData dictData, Boolean flag){
        if (flag){
            return dictDataMapper.insert(dictData);
        }
        return dictDataMapper.updateById(dictData);
    }
}
